<?php
$id = $_GET['id'];
require_once 'dbconnect.php';
if ($_SESSION['level'] != 'guest') {
    $nev = $_SESSION['username'];
    $query = mysql_query("SELECT * FROM vote WHERE vote.ki='$nev' AND vote.mit='$id'") or die(mysql_error());
    $rows = mysql_num_rows($query);
    $sql1 = mysql_query("SELECT COUNT(*) AS db FROM vote WHERE mit='$id'");
    $seged1 = mysql_fetch_array($sql1);
    if ($seged1['db'] == 0) {
        $atlag = 'még nem értékelte senki';
    } else {
        $sql2 = mysql_query("SELECT SUM(pont) AS ossz FROM vote WHERE mit='$id'");
        $seged2 = mysql_fetch_array($sql2);
        $atlag = round($seged2['ossz'] / $seged1['db'], 1);
    }
    if (isset($_POST['vote_button'])) {
        $val = $_POST['vote_point'];
        mysql_query("INSERT INTO vote (ki, mit, pont) VALUES ('$nev', '$id', '$val')") or die(mysql_error());
        echo $atlag;
    } elseif ($rows > 0) {
        echo $atlag;
    } else {
        ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>?show=film&id=<?php echo $id; ?>">
            <select name="vote_point">
                <option value="1">1 (legalacsonyabb)</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10 (legmagasabb)</option>
            </select>
            <input type="submit" name="vote_button" value="Értékelem"/><?php echo $atlag; ?>
        </form>
        <?php
    }
} else {
    $sql1 = mysql_query("SELECT COUNT(*) AS db FROM vote WHERE mit='$id'");
    $seged1 = mysql_fetch_array($sql1);
    if ($seged1['db'] == 0) {
        $atlag = 'még nem értékelte senki';
    } else {
        $sql2 = mysql_query("SELECT SUM(pont) AS ossz FROM vote WHERE mit='$id'");
        $seged2 = mysql_fetch_array($sql2);
        $atlag = round($seged2['ossz'] / $seged1['db'], 1);
    }
    echo $atlag;
}
?>